package view.frontend.order;

import exceptionManager.ExceptionManager;
import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import model.Product;
import view.generaldesign.DesignLayout;

/*
 @author Mathias and Sonny Sandberg
 */
public class OrderAddProduct {

    private JPanel 
            searchPanel,
            panel,
            productPanel,
            scrollPanel;
    private JTextField tFieldName;
    private Order order;
    private JScrollPane scrollPane;

    public OrderAddProduct(Order view) {
        order = view;
        
        panel = new JPanel(null);
        panel.setBounds(0, 0, 300, 500);
        panel.setOpaque(false);
        panel.addComponentListener(new ComponentAdapter()
        {
            @Override
            public void componentShown(ComponentEvent evt)
            {
                doLookUp("");
            }
        });

        buildSearchPanel();
        buildProductPanel();
        
        panel.setVisible(true);
    }
    
    public void buildSearchPanel()
    {
        searchPanel = new JPanel(null);
        searchPanel.setBounds(0, 0, 300, 85);
        searchPanel.setOpaque(false);
        searchPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Tilføj Vare Til Ordre", TitledBorder.DEFAULT_JUSTIFICATION, 
                                                                                TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));

        JLabel lblName = new JLabel("Navn");
        lblName.setBounds(10, 25, 280, 18);
        lblName.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        
        tFieldName = new JTextField("");
        tFieldName.setBounds(10, 50, 280, 25);
        tFieldName.setBorder(DesignLayout.T_FIELD_RED);
        tFieldName.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldName.setFont(DesignLayout.STANDARD);
        tFieldName.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                doLookUp(tFieldName.getText());
            }
        });

        searchPanel.add(lblName);
        searchPanel.add(tFieldName);

        searchPanel.setVisible(true);
        panel.add(searchPanel);
    }
    
    private void buildProductPanel()
    {
        productPanel = new JPanel(null);
        productPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Vælg Vare", TitledBorder.DEFAULT_JUSTIFICATION, 
                                                                                    TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));
        productPanel.setBounds(0, 95, 300, 360);
        productPanel.setOpaque(false);
        
        scrollPanel = new JPanel(null);
        scrollPanel.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        scrollPanel.setBackground(Color.white);
        scrollPanel.setBorder(null);
        
        scrollPane = new JScrollPane();
        scrollPane.setBorder(DesignLayout.T_FIELD_RED);
        scrollPane.setBounds(20, 34, 260, 310);
        scrollPane.setViewportView(scrollPanel);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.getVerticalScrollBar().setUnitIncrement(15);
        
        productPanel.add(scrollPane);
        productPanel.setVisible(true);
        panel.add(productPanel);
    }
    
    public void buildFoundPanel(Product[] product)
    {
        scrollPanel.removeAll();
        scrollPane.getViewport().setViewPosition(new Point(0,0));
        
        int heightAdditionsAdded = 0;
        for (int i = 0; i < product.length; i++)
        {
            OrderProductListPanel panel = new OrderProductListPanel(this, product[i]);
            panel.setBounds(10, 10 + (i * 190) + heightAdditionsAdded*20, 220, 180+panel.getHeightAddition());
            scrollPanel.setPreferredSize(new Dimension(200, 220 + (i * 200) + panel.getHeightAddition()));
            scrollPanel.add(panel);
            if (panel.getHeightAddition() > 0)
            {
                heightAdditionsAdded++;
            }
        }
        
        if (product.length == 0)
        {
            JPanel panel = new JPanel(null);
            panel.setOpaque(false);
            panel.setBounds(10, 10, 220, 130);
            scrollPanel.add(panel);
            
            JLabel label = new JLabel("<html><div style='text-align:center; width: 170px;'><b>Ingen Resultater</b></div></html>");
            label.setBounds(0, 0, 220, 130);
            label.setOpaque(true);
            label.setBorder(DesignLayout.T_FIELD_RED);
            label.setBackground(DesignLayout.T_FIELD_BG_COLOR);
            
            panel.add(label);
        }
        
        scrollPanel.revalidate();
        scrollPanel.repaint();       
    }
    
    public void doLookUp(String searchFor)
    {
        try
        {
            Product[] products = model.handlers.ProductHandler.getProductArrayByString("WHERE sold = 0 AND active = 1 AND (productName LIKE '%"+searchFor+"%' OR productID LIKE '%"+searchFor+"%')" , "ORDER BY productName", "");
            buildFoundPanel(products);
        } 
        catch (SQLException ex)
        {
            ExceptionManager.handleSQLException(ex);
        } 
        catch (ClassNotFoundException ex)
        {
            ExceptionManager.handleClassNotFoundException(ex);
        }
    }

    public Order getOrder()
    {
        return order;
    }

    public JPanel getPanel() {
        return panel;
    }
}